<template>
  <div class="bg">
    <div class="login">
    <h2>一日三餐</h2>
      <el-form ref="login" :model="user" status-icon :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="user.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="user.pwd" placeholder="4-8英文数字字母_注册,字母开头" type="password" autocomplete="off" />
        </el-form-item>
        <el-radio-group v-model="role" class="role">
          <el-radio label="admin" size="large">管理员登录</el-radio>
          <el-radio label="normal" size="large">普通用户登录</el-radio>
        </el-radio-group>
        <el-form-item>
          <el-button type="primary" @click="userLogin">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { userLogin } from '@/api/user'
//
export default {
  name: 'LoginPage',
  data () {
    return {
      role: 'admin',
      user: {
        userName: '',
        pwd: ''
      },
      rules: {
        userName: [
          {
            required: true,
            message: '用户名必填'
          }
        ],
        pwd: [
          {
            required: true,
            message: '密码必填'
          },
          {
            pattern: /[a-zA-Z]\w{3,7}/,
            message: '4-8英文数字字母_注册,字母开头'
          }
        ]
      }
    }
  },
  methods: {
    userLogin () {
      this.$refs.login.validate().then(() => {
        userLogin(this.user).then(res => {
          if (res.data.code === 200) {
            console.log(res)
            // 缓存用户个人信息 用于 持久化
            const { token, role, userInfo } = res.data.data
            localStorage.setItem('token', token)
            localStorage.setItem('role', role)
            localStorage.setItem('userInfo', JSON.stringify(userInfo))
            // 存储到vuex
            this.$store.commit('user/INIT_USER', res.data.data)
            this.$message({
              type: 'success',
              message: res.data.msg,
              duration: 2000,
              onClose: () => {
                this.$router.replace('/home')
              }
            })
            /*  const loading = this.$loading({
              lock: true
            }) */
            // 判断是管理员登录还是普通用户登录
            /*  if (this.role === 'admin') {
              getAuth1({
                role
              }).then(async (res) => {
                if (res.data.code === 200) {
                  // 缓存 返回导航和路由数据 持久化
                  const { menus, authRoutes } = res.data.data
                  sessionStorage.setItem('menus', JSON.stringify(menus))
                  sessionStorage.setItem('authRoutes', JSON.stringify(authRoutes))
                  // 将menus 存储到vuex中
                  this.$store.commit('user/INIT_ROLE_MENUS', menus)
                  // 动态添加当前角色可以访问权限路由
                  // this.$store.commit('user/INIT_USER_ROUTES', authRoutes)
                  await this.$store.dispatch('user/setRoutes', authRoutes)
                }
                this.$nextTick(() => {
                  // 跳转到主页
                  this.$router.replace('/home')
                  this.$message({
                    type: 'success',
                    message: '登陆成功',
                    duration: 1000

                  })
                })
              }).finally(() => {
                loading.close()
              })
            } else {
              getAuth2({
                role
              }).then(async (res) => {
                if (res.data.code === 200) {
                  // 缓存 返回导航和路由数据 持久化
                  const { menus, authRoutes } = res.data.data
                  sessionStorage.setItem('menus', JSON.stringify(menus))
                  sessionStorage.setItem('authRoutes', JSON.stringify(authRoutes))
                  // 将menus 存储到vuex中
                  this.$store.commit('user/INIT_ROLE_MENUS', menus)
                  // 动态添加当前角色可以访问权限路由
                  // this.$store.commit('user/INIT_USER_ROUTES', authRoutes)
                  await this.$store.dispatch('user/setRoutes', authRoutes)
                }
                this.$nextTick(() => {
                  // 跳转到主页
                  this.$router.replace('/home')
                  this.$message({
                    type: 'success',
                    message: '登陆成功',
                    duration: 1000

                  })
                })
              }).finally(() => {
                loading.close()
              })
            } */
          }
        })
      }).catch(() => {
        this.$message({
          type: 'error',
          message: '验证失败'
        })
      })
    },
    resetForm () {

    }
  }
}
</script>

<style lang="scss" scoped>
.bg {
  background-image: url(../../assets/bg.jpg);
  width: 100%;
  height: 100%;
  position: absolute;
  background-size: cover;
  display: flex;

}
.login {
  // margin: auto ;
  margin: 50px 50px 0 900px;
  width: 500px;
  height: 360px;
  background-color: rgba($color: #fff, $alpha: 0.5);
  border-radius: 10px;
  padding: 40px;
  box-sizing: border-box;
}

.login>h2 {
  text-align: center;
  margin-bottom: 30px;
}
.role{
  margin-left:90px;
  margin-bottom:20px
}
</style>
